﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;
using skhukb.Controllers;
using skhukb.Models;

namespace skhukb.BL
{
    public static class LinkHekper
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.MapRoute(
                name: "BbsArticleList",
                url: "BbsArticle/List/{id}/{page}/{size}/{category}/{srchType}/{srchText}",
                defaults: new { controller = "BbsArticle", action = "List", srchText = UrlParameter.Optional }
            );
        }

        // Home
        public static void RenderAction_HomeTop(this HtmlHelper html) // 참조: _Layout.cshtml
        {
            if (html == null) new HomeController().Top();
            html.RenderAction("Top", "Home");
        }

        public static void RenderAction_HomeFooter(this HtmlHelper html) // 참조: _Layout.cshtml
        {
            if (html == null) new HomeController().Footer();
            html.RenderAction("Footer", "Home");
        }

        // Subject
        public static string SubjectDetail(this UrlHelper url, long 주제ID)
        {
            if (url == null) new SubjectController().Detail(ID: 주제ID);
            return url.Action("Detail", "Subject", new { ID = 주제ID });
        }

        public static string SubjectHistory(this UrlHelper url, long 주제ID)
        {
            if (url == null) new SubjectController().History(ID: 주제ID); 
            return url.Action("History", "Subject", new { ID = 주제ID });
        }

        public static string SubjectSort(this UrlHelper url, long 주제ID)
        {
            if (url == null) new SubjectController().Sort(ID: 주제ID);
            return url.Action("Sort", "Subject", new { ID = 주제ID });
        }

        public static string SubjectDelete(this UrlHelper url, long 주제ID)
        {
            if (url == null) new SubjectController().Delete(ID: 주제ID);
            return url.Action("Delete", "Subject", new { ID = 주제ID });
        }

        public static string SubjectCalcTitle(this UrlHelper url)
        {
            if (url == null) new SubjectController().CalcTitle();
            return url.Action("CalcTitle", "Subject");
        }

        public static string SubjectRemoved(this UrlHelper url, long 주제ID)
        {
            if (url == null) new SubjectController().Removed(ID: 주제ID);
            return url.Action("Removed", "Subject", new { ID = 주제ID });
        }

        public static string SubjectTreeView(this UrlHelper url)
        {
            if (url == null) new SubjectController().TreeView();
            return url.Action("TreeView", "Subject");
        }

        // Subject Picker
        public static string SubjectPickerSearch(this UrlHelper url, string pattern)
        {
            if (url == null) new SubjectPickerController().Search(pattern: pattern);
            return url.Action("Search", "SubjectPicker", new { pattern });
        }

        public static string SubjectPickerSearch(this UrlHelper url, string pattern, string callback)
        {
            if (url == null) new SubjectPickerController().Search(pattern: pattern, callback: callback);
            return url.Action("Search", "SubjectPicker", new { pattern, callback });
        }

        // User
        public static string UserLogin(this UrlHelper url)
        {
            if (url == null) new UserController().Login();
            return url.Action("Login", "User");
        }

        public static string UserLogout(this UrlHelper url)
        {
            if (url == null) new UserController().Logout();
            return url.Action("Logout", "User");
        }

        // Article
        public static string ArticleNew(this UrlHelper url, long 주제ID)
        {
            if (url == null) new ArticleController().New(ID: 주제ID);
            return url.Action("New", "Article", new { ID = 주제ID });
        }

        public static string ArticleEdit(this UrlHelper url, long 글ID)
        {
            if (url == null) new ArticleController().Edit(ID: 글ID);
            return url.Action("Edit", "Article", new { ID = 글ID });
        }

        public static string ArticleHistory(this UrlHelper url, long 글ID)
        {
            if (url == null) new ArticleController().History(ID: 글ID);
            return url.Action("History", "Article", new { ID = 글ID });
        }

        public static string ArticleDelete(this UrlHelper url, long 글ID)
        {
            if (url == null) new ArticleController().Delete(ID: 글ID);
            return url.Action("Delete", "Article", new { ID = 글ID });
        }

        public static string ArticleUndelete(this UrlHelper url, long 글ID)
        {
            if (url == null) new ArticleController().Undelete(ID: 글ID);
            return url.Action("Undelete", "Article", new { ID = 글ID });
        }

        // Image
        public static string ImageClean(this UrlHelper url)
        {
            if (url == null) new ImageController().Clean();
            return url.Action("Clean", "Image");
        }

        // Bookmark
        public static string BookmarkList(this UrlHelper url)
        {
            if (url == null) new BookmarkController().List();
            return url.Action("List", "Bookmark");
        }

        // Search
        public static string SearchMain(this UrlHelper url)
        {
            if (url == null) new SearchController().Main();
            return url.Action("Main", "Search");
        }

        public static string SearchNew(this UrlHelper url)
        {
            if (url == null) new SearchController().New();
            return url.Action("New", "Search");
        }

        // File
        public static void RenderAction_FileList(this HtmlHelper html, long articleId, int type) 
        {
            if (html == null) new FileController().List(articleId: articleId, type: type);
            html.RenderAction("List", "File", new { articleId = articleId, type = type });
        }

        public static string FileClean(this UrlHelper url)
        {
            if (url == null) new FileController().Clean();
            return url.Action("Clean", "File");
        }

        // Bbs
        public static string BbsArticleList(this UrlHelper url, int id, int page = 0, int size = 0, int category = 0, int srchType = 0, string srchText = null)
        {
            // Route 설정이 추가 되었음. 이 메소드를 수정하려면, Route 설정도 수정해야함. LinkHekper.RegisterRoutes 메소드 참고.
            if (url == null) new BbsArticleController().List(id: 0, page: 0, size: 0, category: 0, srchType: 0, srchText: null);
            return url.Action("List", "BbsArticle", new { id = id, page = page, size = size, category = category, srchType = srchType, srchText = srchText });
        }

        public static string BbsArticleListInit(this UrlHelper url, int id)
        {
            if (url == null) new BbsArticleController().List(id: 0, page: 0, size: 0, category: 0, srchType: 0, srchText: null);
            return url.Action("List", "BbsArticle", new { id = id, page = 1, size = BL.게시판.페이지크기_디폴트, category = BL.게시판.게시글분류_전체, srchType = BL.게시판.검색_없음 });
        }


        public static string BbsArticleNew(this UrlHelper url, int id)
        {
            if (url == null) new BbsArticleController().New(id: 0);
            return url.Action("New", "BbsArticle", new { id = id });
        }

        public static string BbsArticleEdit(this UrlHelper url, long id)
        {
            if (url == null) new BbsArticleController().New(id: 0);
            return url.Action("Edit", "BbsArticle", new { id = id });
        }

        public static string BbsArticleDetail(this UrlHelper url, long id)
        {
            if (url == null) new BbsArticleController().Detail(id: 0);
            return url.Action("Detail", "BbsArticle", new { id = id });
        }

        public static string BbsEdit(this UrlHelper url, int id)
        {
            if (url == null) new BbsController().Edit(id: 0);
            return url.Action("Edit", "Bbs", new { id = id });
        }

        public static string BbsNew(this UrlHelper url)
        {
            if (url == null) new BbsController().New();
            return url.Action("New", "Bbs");
        }
    }
}